<template>
  <div style="border: 1px solid #ccc;">
    <Editor
      style="height: 500px; overflow-y: hidden;"
      v-model="html"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
    />

  </div>
</template>
<script>
  import Vue from 'vue'
  import { Editor } from '@wangeditor/editor-for-vue'
  import kityformula from "./kityformula";
  import { Boot } from "@wangeditor/editor";
  import formulaModule from "@wangeditor/plugin-formula";

  export default Vue.extend({
    props: {
      content: {
        type: String,
        default: '内容'
      },
    },
    components: { Editor },
    data() {
      return {
        editor: null,
        html: '',
        editorConfig: { placeholder: '请输入内容...' , MENU_CONF: {
          uploadImage: {
            server: process.env.VUE_APP_COURSE_BASE_URL + '/api/osa/upload',
            allowedFileTypes: ['image/*'],
            fieldName: 'file',
            withCredentials: true,
            headers: {
              Authorization: window.localStorage.getItem('authorization'),
            },


          },
          insertImage: {
            onInsertedImage(imageNode) {                    // JS 语法
              if (imageNode == null) return
              const { src, alt, url, href } = imageNode
              console.log('inserted image', src, alt, url, href)
            },
            parseImageSrc:  src => {
              console.log("img: ", src)
              return process.env.VUE_APP_COURSE_BASE_URL + src;
            }, // 也支持 async 函数
          }
        }},
        mode: 'default', // or 'simple'
      }
    },
    methods: {
      onCreated(editor) {
        this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
        this.editor.disable()
        console.log("PRE HTML:", this.content)
        this.editor.setHtml(this.content)
      },
    },

    mounted() {
      this.html = this.content
    },
    beforeDestroy() {
      const editor = this.editor
      if (editor == null) return
      editor.destroy() // 组件销毁时，及时销毁编辑器
    },

  })
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
